package main;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

import entity.Doctor;
import entity.RecordDoctor;

import service.DoctorService;
import service.RecordDoctorService;

public class Menu 
{
	
    private int choiseMainItem;
    private int choiseGroupItem;
    private int choiseSubItem;
    private boolean exitMainTrigger;
    private boolean exitGroupTrigger;
    private boolean exitSubTrigger;
    private int choiseDoctorItem;
    private int choiseRecordItem;
    private Scanner scanner;
    private String inputPacientNumberCard;
    private String inputPacientName;
    private String inputVisitDate;    
    private String pathToRepository;  
    private DoctorService doctorService = new DoctorService();
    private RecordDoctorService recordService = new RecordDoctorService();
	private ArrayList<RecordDoctor> records = new ArrayList<RecordDoctor>();
	private ArrayList<RecordDoctor> subRecords = new ArrayList<RecordDoctor>();

	public Menu(String path) 
		throws 
			ClassNotFoundException, 
			IOException
	{
		
		scanner = new Scanner(System.in);
		pathToRepository = path;
	
	}
	
	// ********************************************************************************************************** start
    // ** public methods ***	
	
	public void InitMainMenu()
    {
		try 
		{
			Doctor doctor = new Doctor();
			
			records = recordService.readFile(pathToRepository, records);
	
			while(!exitMainTrigger)
			{
				System.out.println("=================================");
				System.out.println("|          MAIN  MENU            |");
				System.out.println("=================================");
				System.out.println("| Options:                       |");
				System.out.println("|   1. Create new record         |");
				System.out.println("|   2. Update record             |");
				System.out.println("|   3. Delete record             |");
				System.out.println("|   4. Search by fio             |");
				System.out.println("|   5. Group report              |");
				System.out.println("|   6. Read report               |");
				System.out.println("|   7. Subreport by date         |");
				System.out.println("|   8. Exit                      |");

				System.out.println("=================================");

				try
				{
					choiseMainItem = scanner.nextInt();
					scanner.nextLine();
				}
				catch (Exception e) 
				{
					System.out.println("Input error");
				}

				switch (choiseMainItem) 
				{
    				case 1:
    					doctor = findDoctor();			
    					if (doctor != null)
    					{		 
    						getInputRecordParameters(); 
    						//create and show record
    						if (recordService.validation(records, inputVisitDate, inputPacientName, inputPacientNumberCard))   						
    						{
									recordService.view(recordService.create(records, pathToRepository, doctor, 
											inputVisitDate, inputPacientName, inputPacientNumberCard));

    						}
    					}
    					else
    					{
    						System.out.println("Doctor not found");
    					}   				

    					break;
    				case 2:
    					System.out.println("Update:");
    					if(recordService.view(records))
    					{
    						Integer number = findRecordDoctor(records);
    						if (number != null)
    						{
    							doctor = findDoctor();
    							if (doctor != null)
    							{
    								getInputRecordParameters();        			
    								//update and show record
    								if (recordService.validation(records, inputVisitDate, inputPacientName,
    										inputPacientNumberCard))   						
    								{
    									recordService.view(recordService.update(records, number, doctor, 
    											inputVisitDate, inputPacientName, inputPacientNumberCard));
    								}
    							}
    							else
    							{
    								System.out.println("Doctor not found");
    							}   
    						}
    						else
    						{
    							System.out.println("Records not found");
    						}
    					}
  
    					break;
    				case 3:
    					System.out.println("Delete:");
    					if(recordService.view(records))
    					{
    						Integer number = findRecordDoctor(records);  					
    						if (number != null)
    						{
    						  	recordService.view(recordService.delete(records, number));  					  
    						}
    						else
    						{
    							System.out.println("Records not found");
    						}
    					}
    				
    					break;
    				case 4:
    					System.out.println("Search by fio. Input fio:");
    					String inputFioBySearch = scanner.nextLine();
    					recordService.searchByFio(inputFioBySearch, records);
    				
    					break;
    				case 5:
    					exitGroupTrigger = false;
    					InitGroupMenu();
    					
    					break;
    				case 6:
						recordService.readFile(pathToRepository, records) ;
      					recordService.view(records);

      					break;
    				case 7:
    					exitSubTrigger = false;
						InitSubreportMenu();
						
    					break;
    				case 8:
    					System.out.println("Exit selected");
    					exitMainTrigger = true;
    				
    					break;
    				default:
    					System.out.println("Invalid selection");
    				
    					break;
				}
			}
		} catch (IOException e) 
		{
			System.out.println("IOException");
		} catch (ClassNotFoundException e) 
		{
			System.out.println("ClassNotFoundException");
		}
    	
    }
	
	public void InitGroupMenu()
    {
		
		while(!exitGroupTrigger)
    	{
    		System.out.println("=================================");
    		System.out.println("|       SORT REPORT MENU         |");
    		System.out.println("=================================");
    		System.out.println("|Create sorting according:       |");
    		System.out.println("|   1. Specialty of doctor       |");
    		System.out.println("|   2. FIO of doctor             |");
    		System.out.println("|   3. Days                      |");
    		System.out.println("|   4. Exit from submenu         |");

    		System.out.println("=================================");

    		try 
    		{
    			choiseGroupItem = scanner.nextInt();
    			scanner.nextLine();
    		} 
            catch (Exception e) 
            {
                System.out.println("Input error");
            }

    		switch (choiseGroupItem) 
    		{
    			case 1: 
    				System.out.println("Sort by specialty of doctor:");
    				recordService.sortBySpecialty(records);
    				recordService.view(records);

    				break;
    			case 2:
    				System.out.println("Sort by FIO of doctor:");
    				recordService.sortByName(records);
    				recordService.view(records);
    				
    				break;
    			case 3:
    				System.out.println("Sort by date:");
    				recordService.sortByDate(records);
    				recordService.view(records);
    				
    				break;
    			case 4:
    				System.out.println("Exit from subreport selected");
    				exitGroupTrigger = true;
    				
    				break;
    			default:
    				System.out.println("Invalid selection");
    				
    				break;
    		}
    	}
		
    }
	
	/**
	 * @throws IOException
	 */
	public void InitSubreportMenu() throws IOException
    {
		
		while(!exitSubTrigger)
    	{
    		System.out.println("=================================");
    		System.out.println("|       SUBREPORT MENU           |");
    		System.out.println("=================================");
    		System.out.println("| Subreport by date:             |");
    		System.out.println("|   1. Create                    |");
    		System.out.println("|   2. Save                      |");
    		System.out.println("|   3. Read                      |");
    		System.out.println("|   4. Exit from submenu         |");

    		System.out.println("=================================");

    		try 
    		{
    			choiseSubItem = scanner.nextInt();
    			scanner.nextLine();
    		} 
            catch (Exception e) 
            {
                System.out.println("Input error");
            }
          
    		switch (choiseSubItem) 
    		{
    			case 1: 
    				System.out.println("Create new report by date:");
    				String inputDateBySearch = scanner.nextLine();
    				recordService.searchByDate(inputDateBySearch, records, subRecords);

    				break;
    			case 2:
    				System.out.println("Save report by day:");

    				recordService.writeFile(pathToRepository, subRecords);
    				
    				break;
    			case 3:
    				recordService.readFile(pathToRepository, subRecords);
    				recordService.view(subRecords);
    				
    				break;
    			case 4:
    				System.out.println("Exit from subreport selected");
    				exitSubTrigger = true;
    				
    				break;
    			default:
    				System.out.println("Invalid selection");
    				
    				break;
    		}
    	}
		
    }
	
	// ********************************************************************************************************** end
	
	// ********************************************************************************************************** start
    // ** private methods ***	
	
	/**
	 * @return
	 * @throws IOException
	 * @throws ClassNotFoundException
	 */
	private Doctor findDoctor() 
		throws 
			IOException, 
			ClassNotFoundException
	{
		
		System.out.println("Select number of doctor:");
		// fill info from repository
		doctorService.fillInfo();
		//read info
		ArrayList<Doctor> doctors = doctorService.readInfo();
		//choose a doctor
		try 
		{
			choiseDoctorItem = scanner.nextInt();
			scanner.nextLine();
		} 
        catch (Exception e) 
        {
            System.out.println("Input error");
        }

		return doctorService.findDoctor(choiseDoctorItem, doctors);
		
	}
	
	/**
	 * find record doctor
	 * @param records
	 * @return Integer
	 */
	private Integer findRecordDoctor(ArrayList<RecordDoctor> records) 
	{
		
		System.out.println("Select number of record:");
		//choose a record		
		try 
		{
			choiseRecordItem = scanner.nextInt();
			scanner.nextLine();
		} 
        catch (Exception e) 
        {
            System.out.println("Input error");
        }
		
		if (choiseRecordItem > records.size())
		{
			return null;
		}

		return  --choiseRecordItem;
		
	}
	
	/**
	 * get input data 
	 */
	private void getInputRecordParameters()
	{
		
		System.out.println("Put number of your card:");
		inputPacientNumberCard = scanner.nextLine();
		System.out.println("Put fio:");
		inputPacientName = scanner.nextLine();
		System.out.println("Put date:");
		inputVisitDate = scanner.nextLine();
		
	}
	
	// ********************************************************************************************************** end
	
}